#include <iostream>
#include <mutex>
#include <thread>
#include <condition_variable>

using namespace std;
int main()
{
	mutex mtx;
	condition_variable c;
	bool flag = true;
	int n = 100;

	thread t1([&]() {
		int i = 0;
		while (i < n)
		{
			unique_lock<std::mutex> lock(mtx);
			while (!flag)
			{
				c.wait(lock);
			}

			cout << i << endl;

			flag = false;
			i += 2;

			c.notify_one();
		}
		});

	thread t2([&]() {
		int j = 1;
		while (j < n)
		{
			unique_lock<std::mutex> lock(mtx);
			while (flag)
			{
				c.wait(lock);
			}

			cout << j << endl;

			flag = true;
			j += 2;

			c.notify_one();
		}
		});


	t1.join();
	t2.join();

	return 0;
}